package CommonGui;

import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;

import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

public class ImagePrinter 
{
	private PrinterJob printJob;
	private ImagePainter painter;
	
	// List of printing attributes
	PrintRequestAttributeSet printAttributes =  new HashPrintRequestAttributeSet();  
	
	
	public ImagePrinter()
	{
		painter = null;
	}
	
	
	public ImagePrinter(ImagePainter painter)
	{
		setPainterObject(painter);
	}
	
	
	// Add print attribute
	public void AddAttribute(Attribute attribute)
	{
		printAttributes.add(attribute);
	}
	
	
	// Printing image
	public boolean DoPrint()
	{
		if (IsPainterValid() == false)
			return false;
		
		printJob = PrinterJob.getPrinterJob();
		PageFormat format = new PageFormat();
		format.setOrientation(PageFormat.LANDSCAPE);
		Book book = new Book();
		
		//format = printJob.pageDialog(format);
		book.append(painter, format);
		
	    printJob.setPageable(book);

	    if (printJob.printDialog()) {
	      try {
	        if (printAttributes.isEmpty()) { 
	        	printJob.print(); 
	        } else {
	        	printJob.print(printAttributes);
	        }
	      } catch (Exception PrintException) {
	        PrintException.printStackTrace();
	        return false;
	      }
	    }

		return true;
	}
	
	
	public void setPainterObject(ImagePainter painter)
	{
		this.painter = painter;
	}
	
	
	public boolean IsPainterValid()
	{
		return painter != null;
	}
}